package com.ch.mobilesha.activity;

import java.util.List;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.ch.mobilesha.R;
import com.ch.mobilesha.base.BaseActivity;
import com.ch.mobilesha.base.BaseBean;
import com.ch.mobilesha.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.mobilesha.bean.SHouseBean;
import com.ch.mobilesha.common.CommonResult;
import com.ch.mobilesha.request.GetHouseListRequest;
import com.ch.mobilesha.tools.UtilTools;
import com.ch.mobilesha.tools.ViewTools;

public class HouseListActivity extends BaseActivity {

	private ListView lvHouse;
	private List<BaseBean> list = null;
	private ProgressDialog progressDialog;
	private Handler handler;
	@Override
	protected void handlerTitleBarEvent(int mode) {

	}

	@Override
	protected void initData(Intent intent) {

	}

	@Override
	protected void initView() {
		lvHouse = (ListView) findViewById(R.id.lvHouse);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setViews(R.layout.houselist);
		setTitle(null, "房间列表", "刷新");
		initView();
		final GetHouseListRequest getHouseListRequest =new GetHouseListRequest(mContext, new RequestServerListener() {
			public void onRequestServerEnd(Object result) {
				progressDialog.dismiss();
				JSONObject jsonResult = (JSONObject) result;
				try {
					if (jsonResult.getString("result").equals(
							CommonResult.SUCCESS)) {
						list = UtilTools.getListFromJSONArray(
								jsonResult.getJSONArray("list"),
								new SHouseBean(), SHouseBean.class);
						bindHouse();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			public void onRequestServerBegin() {
				progressDialog = ViewTools.showLoading(mContext, "正在加载，请稍候...");
			}
		});
		handler =new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				getHouseListRequest.execute();
				handler.sendMessageDelayed(new Message(), 30000);
			}
		};
		handler.sendMessage(new Message());
	}

	public void bindHouse() {
		HouseListAdapter adapter =new HouseListAdapter();
		lvHouse.setAdapter(adapter);
	}

	private class HouseListAdapter extends BaseAdapter {
		public int getCount() {
			if (list != null) {
				return list.size();
			}
			return 0;
		}

		public Object getItem(int position) {
			if (list != null) {
				return list.get(position);
			}
			return null;
		}

		public long getItemId(int position) {
			if (list != null) {
				return position;
			}
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (list != null) {
				
			}
			return null;
		}

	}
}
